<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
  <div class="layui-form" lay-filter="form_online">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" lay-verify="required" name="name"
                 value="{{d.params.name || ''}}">
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">所属门店</label>
          <div class="layui-input-inline">
            <select name="store_id" lay-filter="store_id">
            </select>
          </div>
        </div>
      </div>

    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品分类</label>
      <div class="layui-input-inline">
        <select name="category_id" lay-verify="required">
          <option value="">请选择商品分类</option>
          {{# layui.each(d.params.categories, function(index, category){ }}
          {{# if(category.children.length > 0) { }}
          <optgroup label="{{category.name}}">
            {{# layui.each(category.children, function(index, child){ }}
            <option value="{{child.id}}" {{d.params.category_id==child.id?'selected':''}}>{{child.name}}</option>
            {{# }) }}
          </optgroup>
          {{# } else { }}
          <option value="{{category.id}}" {{d.params.category_id==category.id?'selected':''}}>{{category.name}}</option>
          {{# } }}
          {{# }) }}
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label form-required">封面图片</label>
        <div class="layui-input-inline">
          <div id="logo_url"></div>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">资产凭证</label>
        <div class="layui-input-block">
          <div id="asset_image_url"></div>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">排序</label>
      <div class="layui-input-inline" style="width: 80px">
        <input type="number" class="layui-input" lay-verify="required" name="sequence" placeholder="排序"
               value="{{d.params.sequence || 0}}">
      </div>
      <div class="layui-inline layui-form-mid">数字越大，排序越靠前</div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">发行方</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" name="issuer"
                 value="{{d.params.issuer || ''}}">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">作品作者</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" name="author"
                 value="{{d.params.author || ''}}">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">权益类型</label>
      <div class="layui-input-block">
        <input type="text" class="layui-input" name="hold_rights"
               value="{{d.params.hold_rights || ''}}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品简介</label>
      <div class="layui-input-block" style="width: 500px">
        <textarea class="layui-textarea" name="intro">{{d.params.intro || ''}}</textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品详情</label>
      <div class="layui-input-inline" style="width: 600px">
        <div id="description">
          {{d.params.description || ''}}
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-footer">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
          <input type="button" lay-submit lay-filter="submit_edit" value="确认" class="layui-btn">
        </div>
      </div>
    </div>
  </div>
</script>
<script>
  layui.data.sendParams = function (obj) {
    layui.use(['fox_upload', 'wangEditor'], function () {
      let $ = layui.$
        , fox_upload = layui.fox_upload
        , form = layui.form
        , admin = layui.admin
        , editor = layui.wangEditor

      //加载门店
      admin.get('agent-admin/stores', function (res) {
        layui.each(res.data, function (index, store) {
          let opt = new Option(store.name, store.id);
          if (store.id === obj.store_id) {
            opt.selected = true;
            $('select[name="store_id"]').append(opt).attr('disabled', true)
          } else {
            if (store.mobile == obj.publisher_account) {
              $('select[name="store_id"]').append(opt)
            }
          }
        })
        form.render()
      })

      //上传控件初始化
      fox_upload.render({
        elem: '#logo_url'
        , url: 'agent-admin/products/image'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , text: '点击上传'
        , name: 'logo_url'
        , value: obj.logo_url
      })
      fox_upload.render({
        elem: '#asset_image_url'
        , url: 'agent-admin/products/image'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , text: '点击上传'
        , name: 'asset_image_url'
        , value: obj.asset_image_url
      })

      //编辑器
      editor.render({
        elem: "#description"
        , height: 500
        , name: 'description'
        , url: 'editor/images'
      });
    })
  }
</script>
